package st.business.tepth.business.user.busnumber.workholder.maintain.finish;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.MaintainPassInfoBean;
import st.business.tepth.config.ItemFields;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/4/19
 */
public class ClassificationAdapter extends BaseQuickAdapter<MultipleItemEntity, BaseViewHolder> {
    private int mOldPosition = 0;
    private OnItemChooseListener<Integer> mItemChooseListener;

    public ClassificationAdapter(int layoutResId,
                                 @Nullable List<MultipleItemEntity> data,
                                 OnItemChooseListener<Integer> listener) {
        super(layoutResId, data);
        mItemChooseListener = listener;
    }

    @Override
    protected void convert(final BaseViewHolder viewHolder, final MultipleItemEntity entity) {
        final CheckBox checkBox = viewHolder.getView(R.id.cb_name);
        final MaintainPassInfoBean maintainPassInfoBean = entity.getField(ItemFields.ITEM_OBJ);
        checkBox.setText(maintainPassInfoBean.getVehicleSeriesName());
        if (!(Boolean) entity.getField("isSelected")) {
            viewHolder.setBackgroundColor(R.id.layout_container,
                    Color.parseColor("#f2f6f9"));
            checkBox.setTextColor(Color.parseColor("#8fa3b7"));
        } else {
            viewHolder.setBackgroundColor(R.id.layout_container,
                    Color.parseColor("#ffffff"));
            checkBox.setTextColor(Color.parseColor("#333333"));
        }
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOldPosition != viewHolder.getAdapterPosition()) {
                    entity.setField("isSelected", true);
                    viewHolder.setBackgroundColor(R.id.layout_container,
                            Color.parseColor("#ffffff"));
                    checkBox.setTextColor(Color.parseColor("#333333"));
                    MultipleItemEntity old = getItem(mOldPosition);
                    if (old != null) {
                        old.setField("isSelected", false);
                        notifyItemChanged(mOldPosition);
                    }
                    mOldPosition = viewHolder.getAdapterPosition();
                    //回调
                    if (mItemChooseListener != null) {
                        mItemChooseListener.onChoose(viewHolder.getAdapterPosition());
                    }
                }

            }
        });
    }
}
